package service

import (
	"GopherFocus/app/model"
	"context"
)

// 菜单服务
var Menu = menuService{}

type menuService struct{}

const (
	settingTopMenusKey = "TopMenus"
)

// 获取顶部菜单
func (m *menuService) GetTopMenus(ctx context.Context) ([]*model.MenuItem, error) {
	var topMenus []*model.MenuItem
	// 获取的值格式为: "[{"name":"首页","url":"/","target":"","items":null},{"name":"主题","url":"/topic","target":"","items":null}]"
	v, err := Setting.GetVar(ctx, settingTopMenusKey)
	if err != nil {
		return nil, err
	}
	err = v.Struct(&topMenus)
	return topMenus, nil
}

// 根据给定的url检索顶部菜单，给定的url可能只是一个url path
func (m *menuService) GetTopMenuByUrl(ctx context.Context, url string) (*model.MenuItem, error) {
	items, _ := m.GetTopMenus(ctx)
	for _, v := range items {
		if v.Url == url {
			return v, nil
		}
	}
	return nil, nil
}
